<?php

namespace app\store\controller\finance;

use app\store\controller\Controller;
use app\store\model\Order as OrderModel;
use app\store\model\dealer\Order as DealerOrderModel;
use app\store\model\GiftsOrder as GiftsOrderModel;
use app\common\model\divide\Receive as ReceiveModel;
use think\Db;
use think\Session;


/**
 * 订单信息
 * Class Order
 * @package app\store\controller\statistics
 */
class Order extends Controller
{
    /**
     * 商品订单
     * @return mixed
     * @throws \think\Exception
     */
    public function goods()
    {
        // 订单列表
        $model = new OrderModel;

        $list = $model->getFinanceList($order_source = 10,$this->request->param());

        return $this->fetch('goods', compact('list'));

    }
    /**
     * 大额订单
     */
    public function large(){
        //获取该商户总额度
        $wxapp_id=$this->store['wxapp']['wxapp_id'];
        $edu=Db::name('wxapp')->where(['wxapp_id'=>$wxapp_id])->field('money')->find();
        $startTime=$this->request->param('start_time');
        $endTime=$this->request->param('end_time');
        $search=$this->request->param('search');
        $paystatus=$this->request->param('pay_status');
        $flag=0;
        if(!empty($paystatus)){
            if($paystatus!=-1){
                $flag=1;
                $map['pay_status']  = ['=',$paystatus];
            }
        }
        if(!empty($search)){
            $flag=1;
            $map['order_no']  = ['like','%'.$search.'%'];
        }
        if(!empty($startTime)&&!empty($endTime)){
            $flag=1;
            $startTime=strtotime($startTime." 00:00:00");
            $endTime=strtotime($endTime." 23:59:59");
            $map['create_time']  = ['between',[$startTime,$endTime]];
        }
        if(empty($startTime)&&!empty($endTime)){
            $flag=1;
            $endTime=strtotime($endTime." 23:59:59");
            $map['create_time']  = ['<=',$endTime];
        }
        if(!empty($startTime)&&empty($endTime)){
            $startTime=strtotime($startTime." 00:00:00");
            $flag=1;
            $map['create_time']  = ['>',$startTime];
        }
        if($flag==0){
            $map='1=1';
        }
        //订单列表
        $pagenum=$this->request->param('page');
        $page=isset($pagenum)&&is_numeric(input('post.page'))?intval($pagenum):1;
        $limit = 10;
        $offset=($page-1) * $limit;
        $list=Db::name('large_order')
            ->limit($offset,$page)
            ->where($map)
            ->where(['is_delete'=>0]) 
            ->where('wxapp_id','=',$wxapp_id)
            ->order('create_time desc')
            ->paginate(10, false, [
                'query' => \request()->request()
            ]);
        return $this->fetch('large', compact('list','edu'));
    }
    /*
     * 删除大额订单
     */
    public function delLarge(){
        $order_id=input('post.order_id');
        $info=Db::name('large_order')->where(['order_id'=>$order_id])->find();
        if($info){
            if($info['is_delete']==1){
                return ['code'=>0,'msg'=>'该订单已删除'];
            }
            $update=Db::name('large_order')->where(['order_id'=>$order_id])->update(['is_delete'=>1]);
            if($update){
                return ['code'=>1,'msg'=>'删除成功'];
            }
            return ['code'=>0,'msg'=>'删除失败'];
        }
        return ['code'=>0,'msg'=>'删除失败'];
    }
    /*
     * 大额提现
     */
    public function apply(){
        $money=input('post.money');
        if (!is_numeric($money)||!preg_match("/^[1-9][0-9]*$/" ,$money)||empty($money)) {
            return ['code'=>0,'msg'=>'请输入正确的提现金额(大于等于1的正整数)','data'=>[]];
        }
        if ($money<1) {
            return ['code'=>0,'msg'=>'提现金额不能小于1','data'=>[]];
        }
        //获取可提现金额
        $wxapp_id=$this->store['wxapp']['wxapp_id'];

        $edu=Db::name('wxapp')->where(['wxapp_id'=>$wxapp_id])->field('money')->find();
        if($money>$edu['money']){
            return ['code'=>0,'msg'=>'提现金额不能大于总额度金额'];
        }
        //更新总额度数据
        $update=Db::name('wxapp')->where(['wxapp_id'=>$wxapp_id])->setDec('money',$money);
        //写入提现数据
        $data['money']=bcmul($money,'0.01',2);
        $data['apply_status']=10;
        $data['apply_time']=time();
        $data['wxapp_id']=$wxapp_id;
        $data['update_time']=0;
        $data['admin_id']=0;
        $data['back_reason']='';
        $data['is_delete']=0;
        $insert=Db::name('large_withdraw')->insertGetId($data);
        //写入操作日志表
        $logData=[
            'money'=>-1*$data['money'],
            'wxapp_id'=>$wxapp_id,
            'create_time'=>time(),
            'user_id'=>0,
            'admin_id'=>0,
            'type'=>3,
            'order_id'=>$insert,
            'is_delete'=>0,
            'store_user_id'=>$this->store['user']['store_user_id']
        ];
        $insertLog=Db::name('large_log')->insertGetId($logData);
        if($update&&$insert&&$insertLog){
            return ['code'=>1,'msg'=>'申请成功'];
        }
        return ['code'=>0,'msg'=>'申请失败'];
    }

    /*
     * 提现申请列表
     */
    public function applyindex(){
        $wxapp_id=$this->store['wxapp']['wxapp_id'];
        $startTime=$this->request->param('start_time');
        $endTime=$this->request->param('end_time');
        $apply_status=$this->request->param('apply_status');
        $flag=0;
        if(!empty($apply_status)){
            if($apply_status!=-1){
                $flag=1;
                $map['apply_status']  = ['=',$apply_status];
            }
        }

        if(!empty($startTime)&&!empty($endTime)){
            $flag=1;
            $startTime=strtotime($startTime." 00:00:00");
            $endTime=strtotime($endTime." 23:59:59");
            $map['apply_time']  = ['between',[$startTime,$endTime]];
        }
        if(empty($startTime)&&!empty($endTime)){
            $flag=1;
            $endTime=strtotime($endTime." 23:59:59");
            $map['apply_time']  = ['<=',$endTime];
        }
        if(!empty($startTime)&&empty($endTime)){
            $startTime=strtotime($startTime." 00:00:00");
            $flag=1;
            $map['apply_time']  = ['>',$startTime];
        }
        if($flag==0){
            $map='1=1';
        }
        //订单列表
        $pagenum=$this->request->param('page');
        $page=isset($pagenum)&&is_numeric(input('post.page'))?intval($pagenum):1;
        $limit = 10;
        $offset=($page-1) * $limit;
        $list=Db::name('large_withdraw')
            ->limit($offset,$page)
            ->where($map)
            ->where(['is_delete'=>0])
            ->where('wxapp_id','=',$wxapp_id)
            ->order('apply_time desc') 
            ->paginate(10, false, [
                'query' => \request()->request()
            ]);
        return $this->fetch('applyindex', compact('list'));
    }
    /**
     * 商品订单导出
     * @return mixed
     * @throws \think\Exception
     */
    public function goodsExport(){
        // 订单列表
        $model = new OrderModel;

        if (!$model->goodsFinanceExport($order_source = 10,$this->request->param())) {
            return $this->renderError($model->getError() ?: '操作失败');
        }
        return $model->goodsFinanceExport($order_source = 10,$this->request->param());
    }



    /**
     * 消费订单
     * @return mixed
     * @throws \think\Exception
     */
    public function face()
    {

        // 订单列表
        $model = new OrderModel;

        $list = $model->getFinanceList($order_source = 50,$this->request->param());

        return $this->fetch('face', compact('list'));

    }


    /**
     * 消费订单导出
     * @return mixed
     * @throws \think\Exception
     */
    public function faceExport()
    {
        // 订单列表
        $model = new OrderModel;

        if (!$model->faceFinanceExport($order_source = 50,$this->request->param())) {
            return $this->renderError($model->getError() ?: '操作失败');
        }
        return $model->faceFinanceExport($order_source = 50,$this->request->param());


    }


    /**
     * 储值订单
     * @return mixed
     * @throws \think\Exception
     */
    public function card()
    {

        // 订单列表
        $model = new OrderModel;

        $list = $model->getFinanceList($order_source = 40,$this->request->param());

        return $this->fetch('card', compact('list'));

    }


    /**
     * 储值订单导出
     * @return mixed
     * @throws \think\Exception
     */
    public function cardExport()
    {
        // 订单列表
        $model = new OrderModel;

        if (!$model->cardFinanceExport($order_source = 40,$this->request->param())) {
            return $this->renderError($model->getError() ?: '操作失败');
        }
        return $model->cardFinanceExport($order_source = 40,$this->request->param());

    }

    /**
     * 爆品礼订单
     * @return mixed
     * @throws \think\Exception
     */
    public function gifts()
    {
        // 获取全部商品列表
        $model = new GiftsOrderModel;

        $list = $model->getFinanceList($this->request->param());

        return $this->fetch('gifts', compact('list'));

    }


    /**
     * 爆品礼订单导出
     * @return mixed
     * @throws \think\Exception
     */
    public function giftsExport()
    {
        // 订单列表
        $model = new GiftsOrderModel;

        if (!$model->giftsFinanceExport($this->request->param())) {
            return $this->renderError($model->getError() ?: '操作失败');
        }

        return $model->giftsFinanceExport($this->request->param());

    }


    /**
     * 分销订单
     * @return mixed
     * @throws \think\Exception
     */
    public function deacler()
    {
        // 获取全部商品列表
        $model = new DealerOrderModel;
        $list = $model->getFinanceList($this->request->param());

        return $this->fetch('deacler', compact('list'));

    }


    /**
     * 分销订单导出
     * @return mixed
     * @throws \think\Exception
     */
    public function deaclerExport()
    {
        // 获取全部商品列表
        $model = new DealerOrderModel;
        if (!$model->deaclerFinanceExport($this->request->param())) {
            return $this->renderError($model->getError() ?: '操作失败');
        }
        return $model->deaclerFinanceExport($this->request->param());
    }


    /**
     * 分红订单
     * @return mixed
     * @throws \think\Exception
     */
    public function bonus()
    {
        $model = new ReceiveModel();
        $wxapp_id = Session::get('yoshop_store')['wxapp']['wxapp_id'];

        return $this->fetch('bonus', [
            // 充值记录列表
            'list' => $model->getReceiveList($this->request->param(),$wxapp_id),
            // 属性集
            'statusList' => $model::getStatuslist(),

        ]);

    }


    /**
     * 分红记录导出
     * @return mixed
     * @throws \think\Exception
     */
    public function bonusExport()
    {
        $model = new ReceiveModel();
        $wxapp_id = Session::get('yoshop_store')['wxapp']['wxapp_id'];

        if (!$model->bonusFinanceExport($this->request->param(),$wxapp_id)) {
            return $this->renderError($model->getError() ?: '操作失败');
        }
        return $model->bonusFinanceExport($this->request->param(),$wxapp_id);

    }



}